<?

class lego_page extends lego_abstract{
    public $id;
    public $page;
    private $content_handler;
    private $table_name = "lego_pages";
    public $tabs;
    public $template_dir;
    public function getDir(){ return dirname(__FILE__); }
    
    public function init(){
    }
    
    public function __construct($id){
        parent::__construct("lego_page".$id);
    	$this->id = $id;
    	$this->template_dir = $this->getView()->getDir();
        $this->page = new lego_page_m($this->id);
        $this->tabs = new view_radio("tabs", $this->getName().'[child]');
    }
    
    public function hasChilds(){ return $this->has_childs; }
    
    public function setContentHandler($func){
        $this->content_handler = $func;
    }
    
    public function action_index(){
        $content = $this->page['content'];
        if(is_callable($this->content_handler)) $content = call_user_func($this->content_handler, $content);
        if($this->page->isMy())
        	$content = $this->fetch("admin_menu.tpl").$content;
        Output::setContent($content);
        if(class_exists("lego_comments")){
        	$c = new lego_comments(false, 'lego_pages', $this->page->getId());
        	$c->run();
        	Output::appendContent($c->getOutput());
		}
        return $this->fetch("lego_page.tpl");
    }
    
    public function action_child(){
    	$child_name = $this->getActionParam(0);
    	$lego = new lego_page($child_name);
    	$lego->setContentHandler($this->content_handler);
    	$lego->run();
    	$content = $this->fetch("tabs.tpl");
    	
        return "<div class='lego' name='{$this->getName()}'>{$content}<div class='tabContent'>{$lego->getOutput()}</div></div>";
	}
    
    
    public function action_new(){
    	Auth::authorize();
        $f = new lego_form($this->getName()."_lego_form", $this->table_name);
        $f->getForm()->setDefaultValue("title", "Новая страница");
        $f->getForm()->setDefaultValue("entity_name", "lego_page");
        $f->getForm()->setDefaultValue("entity_id", $this->id);
        $f->getForm()->setDefaultValue("user_id", User::getCurrentUserId());
        $f->getForm()->hideAllExceptFields("submit", "content", "title");
        $f->run();
        $out = "<a href='?{$this->combine("lego_page".$this->id."=?")}'>просмотр</a>";
        $out .= $f->getOutput();
        Output::setContent($out);
        return $this->fetch("lego_page.tpl");
    }
    
    public function action_edit(){
    	if(!$this->page->isMy()) $this->_goto($this->actUri('index')->url());
        $f = new lego_form("lego_form{$this->id}", $this->table_name, $this->id, 'id');
        $f->getForm()->hideAllExceptFields("submit", "content", "title");
        $f->run();
        Output::setContent($this->fetch("admin_menu.tpl").$f->getOutput());
        return $this->fetch("lego_page.tpl");
    }
    
    public function action_delete(){
        $this->page->remove();
        $this->_goto($this->uri()->remove($this->getName())->url());
    }
    
    public function action_restore(){
        $this->page->restore();
        $this->_goto($this->uri()->remove($this->getName())->url());
    }
}
?>
